<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Cache;
use think\Model;
class Link extends Model{
    public function listData($tag){
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : 'link_id,link_name,link_logo,link_url,link_oid,link_type';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
        if(!in_array($tag['order'],['link_id asc','link_id desc','link_oid desc','link_oid asc'])){
            $tag['order'] = 'link_oid asc';
        }	
        if(!in_array($tag['type'],['1','2'])){
            $tag['type'] = '';
        }
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		$where['link_status']=['eq',1];
		if(!empty($tag['type'])){
		   $where['link_type']=['eq',$tag['type']];
		}
		$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
		$data = db('Link')->where($where)->field($tag['field'])->order($tag['order'])->limit($limit_str)->select();
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_link')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 
}